package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"go-tools/models"
	"net/http"
)

func main() {

	model := models.Card{}

	prefix := "https://poedb.tw/cn/"
	suffix := model.GetUrlSuffix()
	url := prefix + suffix

	res, _ := http.Get(url)

	defer res.Body.Close()

	doc, _ := goquery.NewDocumentFromReader(res.Body)

	initNumber := 1

	dataList := []models.CardList{}

	for {
		selector := model.GetUrlSelector(initNumber)

		// 如果 selector 为空，退出循环
		if len(selector) == 0 {
			break
		}

		for _, selectorSub := range selector {
			var text []string
			doc.Find(selectorSub).Each(func(i int, s *goquery.Selection) {
				text = append(text, s.Text())
				fmt.Println(s.Text())
			})

			// 确保 text 有足够的元素
			if len(text) >= 3 {
				dataList = append(dataList, models.CardList{
					CardCn:   text[0],
					Number:   text[1],
					Currency: text[2],
				})
			}
		}
		initNumber++
	}

	//models.GetDB().Create(&models.Card{}[dataList])
}
